#ifndef __HDUHANDLER_HEADER
#define __HDUHANDLER_HEADER

#include "gvj.h"
#include "hdu.h"
#include "hdu_task.h"
#include "hdu_parser.h"
#include "http_response.h"
#include "mycurl.h"
#include <pthread.h>
#include <string>

class HduHandler {
private:
	typedef key_t msg_key;
	typedef pthread_mutex_t mutex;

private:
	MyCurl *mycurl;
	int out_pipe;
	int msg_id;
	HduParser parse_html;
	mutex *out_pipe_mutex_ptr;
	HduTask current_task;
	std::string current_result;

private:
	static const char *user_agent;

	static size_t save_http_contents(char *ptr, size_t size, size_t nmemb, void *user_data);
	static std::string login_data(const char *user_id, const char *passwd);
	static std::string submit_data(const char *problemid, const char *language, const char *usercode);
	static std::string find_language_id(const char *language);
public:
	HduHandler(int curl_id, std::string cookie_dir, int outpipe, int msgid, pthread_mutex_t *mutex_ptr);
	~HduHandler();

	int login(const char *user_id, const char *passwd, void *user_data = NULL);
	int submit(HduTask &task);
	void get_judge_result();
	int send_result_to_listen();
};

#endif // __HDUHANDLER_HEADER
